package com.lmz.project.redis.arithmetic.threadLearn;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class CallableTest implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        int sum = 0;
        for (int i = 0; i < 100; i++) {
            if (i%2==0){
                System.out.println(Thread.currentThread().getName()+":\t"+i);
                sum+=i;
            }
        }
        return sum;
    }


    public static void main(String[] args) {
        CallableTest callableTest = new CallableTest();
        FutureTask<Integer> futureTask = new FutureTask<>(callableTest);
        Thread t1 = new Thread(futureTask);
        t1.setName("线程1");
        t1.start();
        Thread t2 = new Thread(futureTask);
        t2.setName("线程2");
        t2.start();

        try {
            Integer sum = futureTask.get();
            System.out.println("总和为"+ sum);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
    }
}
